package com.example.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.MaterialsLossMapper;
import com.example.mapper.OutFormMapper;
import com.example.mapper.WarehouseMapper;
import com.example.model.dtos.PageResult;
import com.example.model.pojo.MaterialsLoss;
import com.example.model.pojo.OutForm;
import com.example.model.pojo.Warehouse;
import com.example.service.OutFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OutFormServiceImpl implements OutFormService {

    @Autowired(required = false)
    OutFormMapper outFormMapper;

    @Autowired(required = false)
    WarehouseMapper warehouseMapper;

    @Autowired(required = false)
    MaterialsLossMapper materialsLossMapper;

    @Override
    public PageResult getAllOutForm(Integer pageStart, Integer pageSize, String outFormId, String startDate, String endDate) {
        //创建分页对象并将当前页码和页面大小写入
        Page<OutForm> outFormPage =new Page<>(pageStart, pageSize);
        QueryWrapper<OutForm> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isBlank(startDate)){
            startDate="1970-01-01";
        }
        if (StringUtils.isBlank(endDate)){
            endDate="2099-01-01";
        }
        DateTime start = DateUtil.parse(startDate);
        DateTime end = DateUtil.parse(endDate);

        queryWrapper.between("date_",start,end);
        if (StringUtils.isNotBlank(outFormId)){
            queryWrapper.like("my_id_", outFormId);
        }
        outFormMapper.selectPage(outFormPage, queryWrapper);
        List<OutForm> records = outFormPage.getRecords();

//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), outFormPage.getTotal(),records);

        return pageResult;
    }

    @Override
    public Boolean addOutForm(OutForm outForm) {
//        当状态类型为2时，为取出，直接减去库存
        if (outForm.getStatus().equals("2")){
            QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<>();
            warehouseQueryWrapper.eq("materials_id_", outForm.getMaterialsId());
            Warehouse warehouse = warehouseMapper.selectOne(warehouseQueryWrapper);
            Integer total = warehouse.getTotal();
            //若取出数量大于库存，直接返回
            if (outForm.getOutAmount()>total){
                return false;
            }
            warehouse.setTotal(total-outForm.getOutAmount());
            warehouseMapper.updateById(warehouse);
        }

        int insert = outFormMapper.insert(outForm);
        return insert!=0;
    }

    @Override
    public PageResult getUnreviewedPage(Integer pageStart, Integer pageSize, String outFormId, String startDate, String endDate) {
        //创建分页对象并将当前页码和页面大小写入
        Page<OutForm> outFormPage =new Page<>(pageStart, pageSize);
        QueryWrapper<OutForm> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isBlank(startDate)){
            startDate="1970-01-01";
        }
        if (StringUtils.isBlank(endDate)){
            endDate="2099-01-01";
        }
        DateTime start = DateUtil.parse(startDate);
        DateTime end = DateUtil.parse(endDate);

        queryWrapper.between("date_",start,end);
        if (StringUtils.isNotBlank(outFormId)){
            queryWrapper.like("my_id_", outFormId);
        }

        queryWrapper.eq("status_", 0);
        outFormMapper.selectPage(outFormPage, queryWrapper);
        List<OutForm> records = outFormPage.getRecords();

//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), outFormPage.getTotal(),records);

        return pageResult;
    }

    @Override
    public Boolean audit(OutForm outForm) {

        if (outForm.getStatus().equals("1")){
            outForm = outFormMapper.selectById(outForm.getMyId());
//        根据物资ID查询库存
            QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<>();
            warehouseQueryWrapper.eq("materials_id_", outForm.getMaterialsId());
            Warehouse warehouse = warehouseMapper.selectOne(warehouseQueryWrapper);
            //若库存小于取出数量，则直接判定为不通过
            if (warehouse.getTotal()<outForm.getOutAmount()){
                outForm.setStatus("-1");
            }
            warehouse.setTotal(warehouse.getTotal()-outForm.getOutAmount());
            warehouseMapper.updateById(warehouse);

        }

        UpdateWrapper<OutForm> outFormUpdateWrapper = new UpdateWrapper();
        outFormUpdateWrapper.eq("my_id_", outForm.getMyId());
        outFormUpdateWrapper.set("status_", outForm.getStatus());
        int insert = outFormMapper.update(null, outFormUpdateWrapper);

        return insert!=0;
    }

    @Override
    public Boolean MaterialsLoss(MaterialsLoss materialsLoss) {

        QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<>();
        warehouseQueryWrapper.eq("materials_id_", materialsLoss.getMaterialsId());
        Warehouse warehouse = warehouseMapper.selectOne(warehouseQueryWrapper);
        if (warehouse == null){
            return false;
        }
        if (warehouse.getTotal()<materialsLoss.getAmount()){
            return false;
        }
        UpdateWrapper<Warehouse> warehouseUpdateWrapper = new UpdateWrapper();
        warehouseUpdateWrapper.set("total_", warehouse.getTotal()-materialsLoss.getAmount());
        warehouseUpdateWrapper.eq("materials_id_", materialsLoss.getMaterialsId());
        int update = warehouseMapper.update(null, warehouseUpdateWrapper);
        if (update==1){
            int insert = materialsLossMapper.insert(materialsLoss);
            return insert!=0;
        }
        return false;
    }
}
